<?php

namespace App\Http\Controllers\Home\Index;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Http\Model\Tags;
use App\Http\Model\Link;
use App\Http\Model\Article;
use App\Http\Model\Comment;

class booklistsController extends Controller
{
    // PHP列表
    public function lists()
    {
        $list = Tags::select()->get()->toArray();
        $res = Link::select()->get()->toArray();
        $result = Article::where(['article_category'=>2])->paginate(15);
        foreach ($result as $k=>$v){
            $time = $v['add_time'];
            $result[$k]['add_time'] = date('Y-m-d',strtotime($time));
            if ($v['article_category'] == 2){
                $result[$k]['article_category'] = 'PHP';
            }
        }
        return view('Home/Index/php_lists')->with(['list'=>$list,'res'=>$res,'result'=>$result]);
    }

   //sql栏目列表
    public function sqlList()
    {
        $list = Tags::select()->get()->toArray();
        $res = Link::select()->get()->toArray();
        $result = Article::where(['article_category'=>3])->paginate(15);
        foreach ($result as $k=>$v){
            $time = $v['add_time'];
            $result[$k]['add_time'] = date('Y-m-d',strtotime($time));
            if ($v['article_category'] == 3){
                $result[$k]['article_category'] = 'SQL';
            }
        }
        return view('Home/Index/sql_list')->with(['list'=>$list,'res'=>$res,'result'=>$result]);
    }

        public function about(){
            return view('Home/Index/about');
        }


    //详情
    public function comment(){
        $id = request('id');
        $list = Tags::select()->get()->toArray();
        $res = Link::select()->get()->toArray();
        $result = Article::where('id',$id)->get()->toArray();
        $comlist = Comment::where('article_id',$id)->orderBy('comment_time','desc ')->get()->toArray();
         Article::where('id',$id)->increment('read_num',1);
        foreach ($comlist as $k=>$val){
            $time = $val['comment_time'];
            $comlist[$k]['comment_time'] = date('Y-m-d',strtotime($time));
        }
        $name = Article::where('id',$id)->first(['article_name']);
        $name =$name->article_name;
        $connection = Article::where('article_name', 'like', "%{$name}%")->get()->toArray();
        foreach ($result as $k=>$v) {
            $time = $v['add_time'];
            $result[$k]['add_time'] = date('Y-m-d',strtotime($time));
            if ($v['article_category'] == 1) {
                $result[$k]['article_category'] = '网站首页';
            }
            if ($v['article_category'] == 2) {
                $result[$k]['article_category'] = 'PHP';
            }
            if ($v['article_category'] == 3) {
                $result[$k]['article_category'] = 'SQL';
            }
            if ($v['article_category'] == 4) {
                $result[$k]['article_category'] = '关于我';
            }
        }
        return view('Home/Index/comment')->with(['list'=>$list,'res'=>$res,'result'=>$result,'connection'=>$connection,'comlist'=>$comlist]);
    }

    //评论文章
     public function comments()
     {
        $id = request('id');
        $comment = request('text');
        $num = rand(1000,9999);
        $uname = '游客'.$num;
        $com = Comment::create(['user_name'=>$uname,'comment'=>$comment,'article_id'=>$id,'comment_time'=>date('Y-m-d')]);
         if ($com) {
             return response()->json(['status'=>200, 'message'=>'评论成功']);
         }
         return response()->json(['status'=>402, 'message'=>'评论失败']);

    }

    //点赞
    public function click(){
        $id = request('id');
       return $result =  Article::where('id',$id)->increment('click',1);

    }
}
